home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2018 July / PCgo 07-2018 CD-ROM Germany.iso / nw.pak / Unnamed File 004917.txt < prev    next >
Encoding:
Text File  |  2015-07-29  |  10.2 KB  |  307 lines

  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // This module implements the attributes of the <webview> tag.
  6.  
  7. var GuestViewInternal =
  8.     require('binding').Binding.create('guestViewInternal').generate();
  9. var WebViewImpl = require('webView').WebViewImpl;
  10. var WebViewConstants = require('webViewConstants').WebViewConstants;
  11. var WebViewInternal = require('webViewInternal').WebViewInternal;
  12.  
  13. // -----------------------------------------------------------------------------
  14. // Attribute objects.
  15.  
  16. // Default implementation of a WebView attribute.
  17. function WebViewAttribute(name, webViewImpl) {
  18.   this.name = name;
  19.   this.webViewImpl = webViewImpl;
  20.   this.ignoreMutation = false;
  21.  
  22.   this.defineProperty();
  23. }
  24.  
  25. // Retrieves and returns the attribute's value.
  26. WebViewAttribute.prototype.getValue = function() {
  27.   return this.webViewImpl.element.getAttribute(this.name) || '';
  28. };
  29.  
  30. // Sets the attribute's value.
  31. WebViewAttribute.prototype.setValue = function(value) {
  32.   this.webViewImpl.element.setAttribute(this.name, value || '');
  33. };
  34.  
  35. // Changes the attribute's value without triggering its mutation handler.
  36. WebViewAttribute.prototype.setValueIgnoreMutation = function(value) {
  37.   this.ignoreMutation = true;
  38.   this.webViewImpl.element.setAttribute(this.name, value || '');
  39.   this.ignoreMutation = false;
  40. }
  41.  
  42. // Defines this attribute as a property on the webview node.
  43. WebViewAttribute.prototype.defineProperty = function() {
  44.   Object.defineProperty(this.webViewImpl.element, this.name, {
  45.     get: function() {
  46.       return this.getValue();
  47.     }.bind(this),
  48.     set: function(value) {
  49.       this.setValue(value);
  50.     }.bind(this),
  51.     enumerable: true
  52.   });
  53. };
  54.  
  55. // Called when the attribute's value changes.
  56. WebViewAttribute.prototype.handleMutation = function(oldValue, newValue) {};
  57.  
  58. // An attribute that is treated as a Boolean.
  59. function BooleanAttribute(name, webViewImpl) {
  60.   WebViewAttribute.call(this, name, webViewImpl);
  61. }
  62.  
  63. BooleanAttribute.prototype.__proto__ = WebViewAttribute.prototype;
  64.  
  65. BooleanAttribute.prototype.getValue = function() {
  66.   return this.webViewImpl.element.hasAttribute(this.name);
  67. };
  68.  
  69. BooleanAttribute.prototype.setValue = function(value) {
  70.   if (!value) {
  71.     this.webViewImpl.element.removeAttribute(this.name);
  72.   } else {
  73.     this.webViewImpl.element.setAttribute(this.name, '');
  74.   }
  75. };
  76.  
  77. // Attribute that specifies whether transparency is allowed in the webview.
  78. function AllowTransparencyAttribute(webViewImpl) {
  79.   BooleanAttribute.call(
  80.       this, WebViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, webViewImpl);
  81. }
  82.  
  83. AllowTransparencyAttribute.prototype.__proto__ = BooleanAttribute.prototype;
  84.  
  85. AllowTransparencyAttribute.prototype.handleMutation = function(oldValue,
  86.                                                                newValue) {
  87.   if (!this.webViewImpl.guest.getId()) {
  88.     return;
  89.   }
  90.  
  91.   WebViewInternal.setAllowTransparency(this.webViewImpl.guest.getId(),
  92.                                        this.getValue());
  93. };
  94.  
  95. // Attribute that used to turn on/off Node
  96. function NWAttribute(webViewImpl) {
  97.   BooleanAttribute.call(
  98.       this, WebViewConstants.ATTRIBUTE_NW, webViewImpl);
  99. }
  100.  
  101. NWAttribute.prototype.__proto__ = BooleanAttribute.prototype;
  102.  
  103. NWAttribute.prototype.handleMutation = function(oldValue,
  104.                                                 newValue) {
  105.   if (!this.webViewImpl.guest.getId()) {
  106.     return;
  107.   }
  108.   // FIXME(roger)
  109.   // WebViewInternal.setAllowTransparency(
  110.   //     this.webViewImpl.guest.getId(),
  111.   //     this.webViewImpl.attributes[
  112.   //         WebViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY].getValue());
  113. };
  114.  
  115. // Attribute used to define the demension limits of autosizing.
  116. function AutosizeDimensionAttribute(name, webViewImpl) {
  117.   WebViewAttribute.call(this, name, webViewImpl);
  118. }
  119.  
  120. AutosizeDimensionAttribute.prototype.__proto__ = WebViewAttribute.prototype;
  121.  
  122. AutosizeDimensionAttribute.prototype.getValue = function() {
  123.   return parseInt(this.webViewImpl.element.getAttribute(this.name)) || 0;
  124. };
  125.  
  126. AutosizeDimensionAttribute.prototype.handleMutation = function(
  127.     oldValue, newValue) {
  128.   if (!this.webViewImpl.guest.getId()) {
  129.     return;
  130.   }
  131.   this.webViewImpl.guest.setAutoSize({
  132.     'enableAutoSize': this.webViewImpl.attributes[
  133.       WebViewConstants.ATTRIBUTE_AUTOSIZE].getValue(),
  134.     'min': {
  135.       'width': this.webViewImpl.attributes[
  136.         WebViewConstants.ATTRIBUTE_MINWIDTH].getValue(),
  137.       'height': this.webViewImpl.attributes[
  138.         WebViewConstants.ATTRIBUTE_MINHEIGHT].getValue()
  139.     },
  140.     'max': {
  141.       'width': this.webViewImpl.attributes[
  142.         WebViewConstants.ATTRIBUTE_MAXWIDTH].getValue(),
  143.       'height': this.webViewImpl.attributes[
  144.         WebViewConstants.ATTRIBUTE_MAXHEIGHT].getValue()
  145.     }
  146.   });
  147.   return;
  148. };
  149.  
  150. // Attribute that specifies whether the webview should be autosized.
  151. function AutosizeAttribute(webViewImpl) {
  152.   BooleanAttribute.call(this, WebViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl);
  153. }
  154.  
  155. AutosizeAttribute.prototype.__proto__ = BooleanAttribute.prototype;
  156.  
  157. AutosizeAttribute.prototype.handleMutation =
  158.     AutosizeDimensionAttribute.prototype.handleMutation;
  159.  
  160. // Attribute that sets the guest content's window.name object.
  161. function NameAttribute(webViewImpl) {
  162.   WebViewAttribute.call(this, WebViewConstants.ATTRIBUTE_NAME, webViewImpl);
  163. }
  164.  
  165. NameAttribute.prototype.__proto__ = WebViewAttribute.prototype
  166.  
  167. NameAttribute.prototype.handleMutation = function(oldValue, newValue) {
  168.   oldValue = oldValue || '';
  169.   newValue = newValue || '';
  170.   if (oldValue === newValue || !this.webViewImpl.guest.getId()) {
  171.     return;
  172.   }
  173.  
  174.   WebViewInternal.setName(this.webViewImpl.guest.getId(), newValue);
  175. };
  176.  
  177. // Attribute representing the state of the storage partition.
  178. function PartitionAttribute(webViewImpl) {
  179.   WebViewAttribute.call(
  180.       this, WebViewConstants.ATTRIBUTE_PARTITION, webViewImpl);
  181.   this.validPartitionId = true;
  182. }
  183.  
  184. PartitionAttribute.prototype.__proto__ = WebViewAttribute.prototype;
  185.  
  186. PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) {
  187.   newValue = newValue || '';
  188.  
  189.   // The partition cannot change if the webview has already navigated.
  190.   if (!this.webViewImpl.beforeFirstNavigation) {
  191.     window.console.error(WebViewConstants.ERROR_MSG_ALREADY_NAVIGATED);
  192.     this.setValueIgnoreMutation(oldValue);
  193.     return;
  194.   }
  195.   if (newValue == 'persist:') {
  196.     this.validPartitionId = false;
  197.     window.console.error(
  198.         WebViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE);
  199.   }
  200. };
  201.  
  202. // Attribute that handles the location and navigation of the webview.
  203. function SrcAttribute(webViewImpl) {
  204.   WebViewAttribute.call(this, WebViewConstants.ATTRIBUTE_SRC, webViewImpl);
  205.   this.setupMutationObserver();
  206. }
  207.  
  208. SrcAttribute.prototype.__proto__ = WebViewAttribute.prototype;
  209.  
  210. SrcAttribute.prototype.setValueIgnoreMutation = function(value) {
  211.   // takeRecords() is needed to clear queued up src mutations. Without it, it is
  212.   // possible for this change to get picked up asyncronously by src's mutation
  213.   // observer |observer|, and then get handled even though we do not want to
  214.   // handle this mutation.
  215.   this.observer.takeRecords();
  216.   this.ignoreMutation = true;
  217.   this.webViewImpl.element.setAttribute(this.name, value || '');
  218.   this.ignoreMutation = false;
  219. }
  220.  
  221. SrcAttribute.prototype.handleMutation = function(oldValue, newValue) {
  222.   // Once we have navigated, we don't allow clearing the src attribute.
  223.   // Once <webview> enters a navigated state, it cannot return to a
  224.   // placeholder state.
  225.   if (!newValue && oldValue) {
  226.     // src attribute changes normally initiate a navigation. We suppress
  227.     // the next src attribute handler call to avoid reloading the page
  228.     // on every guest-initiated navigation.
  229.     this.setValueIgnoreMutation(oldValue);
  230.     return;
  231.   }
  232.   this.parse();
  233. };
  234.  
  235. // The purpose of this mutation observer is to catch assignment to the src
  236. // attribute without any changes to its value. This is useful in the case
  237. // where the webview guest has crashed and navigating to the same address
  238. // spawns off a new process.
  239. SrcAttribute.prototype.setupMutationObserver =
  240.     function() {
  241.   this.observer = new MutationObserver(function(mutations) {
  242.     $Array.forEach(mutations, function(mutation) {
  243.       var oldValue = mutation.oldValue;
  244.       var newValue = this.getValue();
  245.       if (oldValue != newValue) {
  246.         return;
  247.       }
  248.       this.handleMutation(oldValue, newValue);
  249.     }.bind(this));
  250.   }.bind(this));
  251.   var params = {
  252.     attributes: true,
  253.     attributeOldValue: true,
  254.     attributeFilter: [this.name]
  255.   };
  256.   this.observer.observe(this.webViewImpl.element, params);
  257. };
  258.  
  259. SrcAttribute.prototype.parse = function() {
  260.   if (!this.webViewImpl.elementAttached ||
  261.       !this.webViewImpl.attributes[
  262.         WebViewConstants.ATTRIBUTE_PARTITION].validPartitionId ||
  263.       !this.getValue()) {
  264.     return;
  265.   }
  266.  
  267.   if (!this.webViewImpl.guest.getId()) {
  268.     if (this.webViewImpl.beforeFirstNavigation) {
  269.       this.webViewImpl.beforeFirstNavigation = false;
  270.       this.webViewImpl.createGuest();
  271.     }
  272.     return;
  273.   }
  274.  
  275.   // Navigate to |src|.
  276.   WebViewInternal.navigate(this.webViewImpl.guest.getId(), this.getValue());
  277. };
  278.  
  279. // -----------------------------------------------------------------------------
  280.  
  281. // Sets up all of the webview attributes.
  282. WebViewImpl.prototype.setupWebViewAttributes = function() {
  283.   this.attributes = {};
  284.  
  285.   this.attributes[WebViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY] =
  286.       new AllowTransparencyAttribute(this);
  287.   this.attributes[WebViewConstants.ATTRIBUTE_AUTOSIZE] =
  288.       new AutosizeAttribute(this);
  289.   this.attributes[WebViewConstants.ATTRIBUTE_NAME] =
  290.       new NameAttribute(this);
  291.   this.attributes[WebViewConstants.ATTRIBUTE_PARTITION] =
  292.       new PartitionAttribute(this);
  293.   this.attributes[WebViewConstants.ATTRIBUTE_SRC] =
  294.       new SrcAttribute(this);
  295.   this.attributes[WebViewConstants.ATTRIBUTE_NW] =
  296.       new NWAttribute(this);
  297.  
  298.   var autosizeAttributes = [WebViewConstants.ATTRIBUTE_MAXHEIGHT,
  299.                             WebViewConstants.ATTRIBUTE_MAXWIDTH,
  300.                             WebViewConstants.ATTRIBUTE_MINHEIGHT,
  301.                             WebViewConstants.ATTRIBUTE_MINWIDTH];
  302.   for (var i = 0; autosizeAttributes[i]; ++i) {
  303.     this.attributes[autosizeAttributes[i]] =
  304.         new AutosizeDimensionAttribute(autosizeAttributes[i], this);
  305.   }
  306. };
  307.